var net = "http://palerock.cn/h5_back";
//                var net = "http://localhost:8080";
//读取本地缓存
var token = localStorage.getItem("token");

if (!token) {

    location.href = "login.html";
}

var app = angular.module('app', [])
    .config(function ($httpProvider) {
        //设置请求头附带 token
        $httpProvider.defaults.headers.common.Authorization = 'Bearer ' + token;
    });

app.controller('user_data', function ($scope, $http) {

    $scope.userInfo = {
        userAge:"",
        userAvatar:"",
        userDetail:"",
        userId:"",
        userNickName:"",
        userPhoneNumber:"",
        userSex:""
    };

    $scope.userBirthy = "";
    $scope.$watch('userBirthy',function(){
        var mydate = new Date();
        var str = "" + mydate.getFullYear();

        $scope.userInfo.userAge= parseInt(str)- parseInt($scope.userBirthy);
        console.log(str);
        console.log( $scope.userBirthy);
    });
    $scope.img_upload = function (files) {

        if (!files || !files[0]) {
            return;
        }

        /* //FileReader用来读取本地文件
         var reader = new FileReader();

         //实例化图片用于被加载在画布上
         var img = new Image();

         //获取预览画布
         var canvas = document.getElementById('img');
         var ctx = canvas.getContext("2d");

         //当图片加载完毕执行动作
         reader.onload = function () {

         // 通过 reader.result 来访问生成的 DataURL
         img.src = reader.result;

         //获取图片的高宽
         var width = img.width;
         var height = img.height;

         //获取需要使用的正方形图片区域
         var real_width = width > height ? height : width;
         var real_height = width > height ? height : width;

         //定位使图片居中
         var img_x = (width - real_width) / 2;
         var img_y = (height - real_height) / 2;

         //清除原来画布内容
         ctx.clearRect(0, 0, 100, 100);

         //在画布上渲染头像预览
         ctx.drawImage(img, img_x, img_y, real_width, real_height, 0, 0, 100, 100);

         //将画布渲染的图像转换为base64字符串储存在data中
         var data = canvas.toDataURL();

         //发送POST请求
         $http({

         method: 'post',
         url: net + "/user/image-upload",
         data: data

         }).then(function (res) {

         if (!res.data.error) {

         console.log(res);
         alert(res.data.mes);

         //将获得的图片名暂时保存
         $scope.userInfo.userAvatar = res.data.data;

         } else {

         alert('错误代码：' + res.data.error + "," + res.data.mes);
         }


         }, function () {

         alert('网络或设置错误');

         })
         };

         //加载选中的图片
         reader.readAsDataURL(files[0]);*/
    };

    //获得用户信息
    $scope.getInfo = function () {
        if (token) {
            $http.get(net + '/user/show-user-info').then(function (res) {
                console.log(res);
                if (!res.data.error) {

                    //将获取的用户信息绑定给userInfo
                    $scope.userInfo = res.data.data;

                    //因为userAvatar是只有图片文件名
                    //所以需要在前面加上服务器储存图片的地址，并自定义一个userAvatarAddress来储存
                    //地址：服务器路径+"/resources/img/"
                    $scope.userInfo.userAvatarAddress = net + "/resources/img/" + res.data.data.userAvatar;

                    //从服务器获得的性别数据不可以直接展示所以自定一个userSexWord来储存性别
                    //0:男，1:女
                    $scope.userInfo.userSexWord = $scope.userInfo.userSex == 0 ? "男" : "女";
                } else {
                    alert('错误代码：' + res.data.error);
                }
            }, function () {
                alert('网络或设置错误');
            })
        }
    };

    //编辑用户信息
    $scope.editInfo = function () {
        if (token) {

            //处理json对象
            //因为更改的json属性是我们自定义的userSexWord，在修改时我们需要将其转化为0或1
            //0:男，1:女
            $scope.userInfo.userSex = $scope.userInfo.userSexWord == "男" ? 0 : 1;

            console.log($scope.userInfo);

            //发送POST请求
            $http.post(net + '/user/edit-user-info', $scope.userInfo).then(function (res) {
                console.log(res);
                if (!res.data.error) {

                    alert(res.data.mes);
                    //刷新用户信息
                    $scope.getInfo();

                } else {
                    alert('错误代码：' + res.data.error + "," + res.data.mes);
                }
            }, function () {
                alert('网络或设置错误');
            })
        }
    };


    $scope.logout = function () {
        localStorage.removeItem("token");
        alert("您已注销成功");
        location.reload();
    }
});